package com.lagou.controller;


import com.lagou.domain.PromotionSpace;
import com.lagou.domain.ResponseResult;
import com.lagou.service.PromotionSpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/PromotionSpace")
public class PromotionSpaceController {

    @Autowired
    private PromotionSpaceService promotionSpaceService;

    /**
     * 查看所有广告列表
     * @return
     */
    @RequestMapping("/findAllPromotionSpace")
    public ResponseResult findAllPromotionSpace(){

        List<PromotionSpace> all = promotionSpaceService.findAll();

        ResponseResult responseResult = new ResponseResult(true, 200, "查询所有广告位成功", all);

        return responseResult;
    }


    /**
     * 添加&修改广告位
     */
    @RequestMapping("/saveOrUpdatePromotionSpace")
    public ResponseResult saveOrUpdatePromotionSpace(@RequestBody PromotionSpace promotionSpace){

        if (promotionSpace.getId() == null) {
            //新增
            promotionSpaceService.savePromotionSpace(promotionSpace);

            ResponseResult responseResult = new ResponseResult(true, 200, "添加广告位成功", null);

            return responseResult;
        }else {
            //更新
            promotionSpaceService.updatePromotionSpace(promotionSpace);

            ResponseResult responseResult = new ResponseResult(true, 200, "修改广告位成功", null);

            return responseResult;
        }

    }

    /**
     * 根据id回显广告位信息
     */
    @RequestMapping("/findPromotionSpaceById")
    public ResponseResult findPromotionSpaceById(Integer id){

        PromotionSpace promotionSpaceById = promotionSpaceService.findPromotionSpaceById(id);

        ResponseResult responseResult = new ResponseResult(true, 200, "回显广告位成功", promotionSpaceById);

        return responseResult;
    }


}
